//
//  SettingController.m
//  AutoMate
//
//  Created by hao on 2018/2/27.
//  Copyright © 2018年 songlei. All rights reserved.
//

#import "SettingBaseController.h"

@interface SettingBaseController ()

@end

@implementation SettingBaseController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark - SettingBaseProtocol
- (UITableViewCellStyle)getTableViewCellStyle
{
    return UITableViewCellStyleValue1;
}

- (UITableViewStyle)getTableViewStyle
{
    return UITableViewStylePlain;
}

- (CGFloat)cellRoundedCorners
{
    return 0.0;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groupModelArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groupModelArr[section].itemsArr.count;
}

- (SettingCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingItemModel *itemModel = self.groupModelArr[indexPath.section].itemsArr[indexPath.row];
    SettingCell *cell = [SettingCell cellWithTableView:tableView indexPath:indexPath itemModel:itemModel];
//    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    if ([self respondsToSelector:@selector(customContentViewWithCell:indexPath:item:)]) {
        [self customContentViewWithCell:cell indexPath:indexPath item:itemModel];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.groupModelArr[indexPath.section].itemsArr[indexPath.row].rowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"didSelectRowAt Section = %zd, row = %zd", indexPath.section, indexPath.row);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.groupModelArr.count == 0) {
        return ;
    }
    SettingCell *cell = (SettingCell *)[tableView cellForRowAtIndexPath:indexPath];
    //获取cell模型
    SettingItemModel *cellItem = self.groupModelArr[indexPath.section].itemsArr[indexPath.row];
    if (cellItem.rightViewClickBlock) {
        cellItem.rightViewClickBlock(cell);
        return ;
    }
    if (cellItem.cellOptionBlock) {  //点击有操作，执行操作
        cellItem.cellOptionBlock(cell);
        return ;
    }
//    UIViewController *destVC = cellItem.targetController;
    if (cellItem.targetController) { //有目标控制器,跳转到目标控制器
        UIViewController *destVC = [[cellItem.targetController alloc] init];
        if (self.navigationController) {
            //隐藏底部tabBar
            destVC.hidesBottomBarWhenPushed = YES;
            destVC.navigationItem.title = cellItem.title;
            [self.navigationController pushViewController:destVC animated:YES];
        } else {
            [self presentViewController:destVC animated:YES completion:nil];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return self.groupModelArr[section].groupHeadHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return self.groupModelArr[section].groupFootHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.groupModelArr[section].groupHeadView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return self.groupModelArr[section].groupFootView;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.groupModelArr[section].groupHeadTitle;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return self.groupModelArr[section].groupFootTitle;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat roundedCorners = [self cellRoundedCorners];
    if (roundedCorners > 0) {
        [self addRoundedCornersWithRadius:roundedCorners forTableView:tableView forCell:cell atIndexPath:indexPath];
    }
}

/*!
 *  添加 cell 圆角 与 边线（willDisplayCell 中添加）
 *
 *  @param cornerRadius    圆角
 *  @param tableView tableView
 *  @param cell      cell
 *  @param indexPath index
 */
- (void)addRoundedCornersWithRadius:(CGFloat)cornerRadius forTableView:(UITableView *)tableView forCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(tintColor)]) {
        cell.backgroundColor = UIColor.clearColor;
        CAShapeLayer *layer = [[CAShapeLayer alloc] init];
        CGMutablePathRef pathRef = CGPathCreateMutable();
        // 参数二width减小的距离， 参数三是height
        CGRect bounds = CGRectInset(cell.bounds, 0, 0);
        BOOL addLine = NO;
        if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
            CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
        } else if (indexPath.row == 0) {
            
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
            addLine = YES;
            
        } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
        } else {
            CGPathAddRect(pathRef, nil, bounds);
            addLine = YES;
        }
        layer.path = pathRef;
        CFRelease(pathRef);
        //颜色修改
        layer.fillColor = cell.backgroundColor.CGColor;
        // 边框颜色
//        layer.strokeColor = [UIColor blackColor].CGColor;
        if (addLine) {
            CALayer *lineLayer = [[CALayer alloc] init];
            CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
            lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
            lineLayer.backgroundColor = cell.backgroundColor.CGColor;
            [layer addSublayer:lineLayer];
        }
        UIView *testView = [[UIView alloc] initWithFrame:bounds];
        [testView.layer insertSublayer:layer atIndex:0];
        testView.backgroundColor = UIColor.clearColor;
        cell.backgroundView = testView;
    }
}

- (CGFloat)tableViewMargin
{
    return 0.0;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//    NSLog(@"%s", __func__);
    [self.view endEditing:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:[self getTableViewStyle]];
        [self.view addSubview:tableView];
        tableView.delegate = self;
        tableView.dataSource = self;
//        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        tableView.separatorColor = UIColorWithRGB(21, 27, 34);
//        tableView.separatorInset = UIEdgeInsetsMake(0, 16, 0, 16);
        tableView.backgroundColor = [UIColor clearColor];
        CGFloat margin = [self tableViewMargin];
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        BOOL iPhone_X = (screenSize.width == 375.f && screenSize.height == 812.f ? YES : NO);
        CGFloat y = iPhone_X ? (44.f + 44.f) : (20.f + 44.f);
        if (!self.navigationController || self.navigationController.navigationBar.hidden) {
            y = iPhone_X ? 44.f : 20.f;
        }
        CGFloat height = self.view.bounds.size.height-y;
        UITabBar *tabBar = self.tabBarController.tabBar;
        if (tabBar && !tabBar.hidden) {
            height -= (iPhone_X ? (34.f+49.f) : 49.f);
        }
        tableView.frame = CGRectMake(margin, y, self.view.bounds.size.width-2*margin, height);
        /** iOS11默认开启Self-Sizing，关闭Self-Sizing才会执行tableView:heightForFooterInSection:方法 */
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        _tableView = tableView;
    }
    return _tableView;
}

- (NSMutableArray<SettingGroupModel *> *)groupModelArr
{
    if (!_groupModelArr) {
        _groupModelArr = [NSMutableArray array];
    }
    return _groupModelArr;
}

@end
